package com.yricky.memoria.ui.component.common

import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.target.ViewTarget
import com.bumptech.glide.request.transition.Transition
import com.yricky.memoria.R
import com.yricky.memoria.utils.Executor
import com.yricky.memoria.utils.PdfDoc
import com.yricky.memoria.utils.mainThreadHandler

/**
 * @author Yricky
 * @date 2021/12/9
 */
//TODO Use glide to load image async
@Composable
fun PathImage(modifier: Modifier = Modifier
    .wrapContentHeight()
    .fillMaxWidth(), imagePath:String){
    AndroidView(
        factory = {
            ImageView(it).apply{
                scaleType = ImageView.ScaleType.CENTER_INSIDE
            }
        },
        modifier = modifier,
        update = {
            Glide.with(it)
                .load(imagePath)
                .error(R.drawable.ic_round_error_48)
                .into(it)
        }
    )
}

@Composable
fun BitmapImage(modifier: Modifier = Modifier
    .wrapContentHeight()
    .fillMaxWidth(),bitmap: Bitmap){
    AndroidView(
        factory = {
            ImageView(it).apply{
                scaleType = ImageView.ScaleType.CENTER_INSIDE
            }
        },
        modifier = modifier,
        update = {

            Glide.with(it.context)
                .load(bitmap)
                .error(R.drawable.ic_round_error_48)
                .into(it)
        }
    )
}

@Composable
fun PdfPreview(modifier: Modifier = Modifier
    .wrapContentHeight()
    .wrapContentWidth(), pdf:PdfDoc, pageIndex:Int, width:Int = 240, height:Int = 240){
    var doc:PdfDoc by remember { mutableStateOf(pdf) }
    var index by remember { mutableStateOf(-1) }
    var cachedBitmap:Bitmap? by remember { mutableStateOf(null) }
    if((pdf != doc) ||
        (index != pageIndex)){
        doc = pdf
        index = pageIndex
        doc.getPage(
            index,
            width,
            height
        ){ bitmap ->
            cachedBitmap?.recycle()
            cachedBitmap = bitmap
        }
    }

    if(cachedBitmap?.let{
        Image(modifier = modifier, bitmap = it.asImageBitmap(), contentDescription = "")
        it
    } == null){
        Image(modifier = Modifier.width(96.dp).height(96.dp), imageVector = ImageVector.vectorResource(id = R.drawable.ic_round_pdf_24), contentDescription = "")
    }
}